package net.wenku8.reader.api

import net.wenku8.reader.entity.ApiResult
import net.wenku8.reader.model.book.BookCase
import net.wenku8.reader.model.book.BookCaseItem
import net.wenku8.reader.model.book.BookDataList
import net.wenku8.reader.model.book.BookInfo
import net.wenku8.reader.model.book.BookPreviewInfo
import net.wenku8.reader.model.book.Volume
import net.wenku8.reader.model.book.VolumeList
import net.wenku8.reader.model.book.VolumeList2
import net.wenku8.reader.model.rank.SearchResult
import net.wenku8.reader.util.ValidateRule
import net.wenku8.reader.util.checkRule
import net.wenku8.reader.util.parseXml

class BookApi {

    companion object {

        fun getBookCase(): ApiResult<ArrayList<BookCaseItem>?> {
            val map = mapOf(
                "action" to "bookcase",
                "t" to "0"
            )
            val apiResult = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResult)){
                return ApiResult.fail(apiResult.message)
            }
            val bookCase = parseXml(apiResult.data, BookCase::class.java)
            return ApiResult.success(bookCase?.list)
        }

        fun addBookCase(bookId : String): ApiResult<String> {
            val map = mapOf(
                "action" to "bookcase",
                "do" to "add",
                "aid" to bookId,
            )
            return Wenku8Connect.request(map)
        }

        fun delBookCase(bookId : String): ApiResult<String> {
            val map = mapOf(
                "action" to "bookcase",
                "do" to "del",
                "aid" to bookId,
            )
            return Wenku8Connect.request(map)
        }

        fun getBookCover(bookId: String): String {
            return "https://img.wenku8.com/image/${bookId.toInt() / 1000}/$bookId/${bookId}s.jpg"
        }

        fun getBookInfo(bookId: String): ApiResult<BookInfo?> {
            val map = mapOf(
                "action" to "book",
                "do" to "meta",
                "aid" to bookId,
            )
            val apiResult = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResult)){
                return ApiResult.fail(apiResult.message)
            }
            val bookDataList = parseXml(apiResult.data, BookDataList::class.java)
            val bookInfo = dataAsBook(bookDataList)
            return ApiResult.success(bookInfo)
        }

        fun getBookVolume(bookId: String): ApiResult<ArrayList<Volume>?> {
            val map = mapOf(
                "action" to "book",
                "do" to "list",
                "aid" to bookId,
            )
            val apiResult = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResult)){
                return ApiResult.fail(apiResult.message)
            }
            val volumeList = parseXml(apiResult.data, VolumeList::class.java)
            val volumeList2 = parseXml(apiResult.data, VolumeList2::class.java)
            mergeBookTitle(volumeList, volumeList2)
            return ApiResult.success(volumeList?.volumeList)
        }

        fun getBookDesc(bookId: String): ApiResult<String> {
            val map = mapOf(
                "action" to "book",
                "do" to "intro",
                "aid" to bookId,
            )
            return Wenku8Connect.request(map)
        }

        fun getBookText(bookId: String, chapterId : String): ApiResult<String> {
            val map = mapOf(
                "action" to "book",
                "do" to "text",
                "aid" to bookId,
                "cid" to chapterId,
            )
            return Wenku8Connect.request(map)
        }


        fun searchBook(searchType: String, searchKey : String): ApiResult<ArrayList<BookPreviewInfo>> {
            val ruleList = listOf(
                ValidateRule(searchKey, "关键词", required = true),
            )
            val msg = checkRule(ruleList)
            if (!msg.isNullOrBlank()){
                return ApiResult.fail(msg)
            }
            val map = mapOf(
                "action" to "search",
                "searchtype" to searchType,
                "searchkey" to searchKey,
            )
            val apiResult = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResult)){
                return ApiResult.fail(apiResult.message)
            }
            val searchList = parseXml(apiResult.data, SearchResult::class.java)
            val list = searchAsPreview(searchList)
            return ApiResult.success(list)
        }

        fun bookVote(bookId : String): ApiResult<String> {
            val map = mapOf(
                "action" to "book",
                "do" to "vote",
                "aid" to bookId,
            )
            return Wenku8Connect.request(map)
        }



    }
}